﻿//********************************************************************************//
//创建日期：<创建日期，2016-06-07>
//创建作者：<郭登杰>
//功能说明：来函公共控件-退款申请信息
//********************************************************************************//
using BLL.Access;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web.Class;
using Entity;
using BLL;
using DevExpress.Web;

namespace Web.Controls.LAIHAN
{
    public partial class UCLHShenQingXX_TK : System.Web.UI.UserControl, IUCLHOperate
    {
        #region 属性

        /// <summary>
        /// 来函类型
        /// </summary>
        public eLaiHanLX LaiHanLX { get; set; }

        /// <summary>
        /// 来函流程
        /// </summary>
        public eLaiHanLCBH LaiHanLC { get; set; }
                
        /// <summary>
        /// 记录站点
        /// </summary>
        public string ZhanDianBH
        {
            get
            {
                return Convert.ToString(ViewState["ZhanDianBH"]);
            }
            set
            {
                ViewState["ZhanDianBH"] = value;
            }
        }

        /// <summary>
        /// 记录管理块
        /// </summary>
        public string GuanLiKBH
        {
            get
            {
                return Convert.ToString(ViewState["GuanLiKBH"]);
            }
            set
            {
                ViewState["GuanLiKBH"] = value;
            }
        }      

        #endregion

        #region 事件

        /// <summary>
        /// 页面加载事件
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitControl();
                SetDisplay();
            }            
        }   

        /// <summary>
        /// 响应前台回调
        /// </summary>
        protected void ASPxCallback1_Callback(object source, DevExpress.Web.CallbackEventArgs e)
        {
            //获取合同号
           
            //e.Result = e.Parameter + "|0|" + hetongh;
        }

        #endregion

        #region 方法

        /// <summary>
        /// 配置控件
        /// </summary>
        public void InitControl()
        {
            //加载退款类型
            WordsManager manger = new WordsManager();
            PageTools.BindComboBox(this.cmbTuiKuanLX, manger.Instance.FindWordsDataTableWithMiddleAffixByBelongID(WordsStruct.BelongID.退款类型,"_"));
            this.cmbTuiKuanLX.SelectedIndex = 0;
        }

        /// <summary>
        /// 设置控件启禁用
        /// </summary>
        /// <param name="enabled"></param>
        private void SetEnabled(bool enabled)
        {
            this.txtTuiKuanJE.Enabled = enabled;
            this.btnOpenFP.Visible = enabled;
            this.cmbTuiKuanLX.Enabled = enabled;
            this.txtYinHangZH.Enabled = enabled;
            this.txtKaiHuYH.Enabled = enabled;
            this.txtKaiHuHM.Enabled = enabled;
            if (this.LaiHanLC < eLaiHanLCBH.登记)
            {
                PageTools.SetDisabled(this.txtTuiKuanJHL, enabled);
                PageTools.SetDisabled(this.txtYSJ, enabled);
                PageTools.SetDisabled(this.txtTuiKuanFP, true);
            }
            else
            {
                this.txtTuiKuanJHL.Enabled = enabled;
                this.txtYSJ.Enabled = enabled;
                this.txtTuiKuanFP.Enabled = enabled;
            }
        }

        /// <summary>
        ///根据当前流程状态设置步骤显示
        /// </summary>
        private void SetDisplay()
        {
            //先隐藏所有
            
            //根据来函显示面板
            //根据来函类型及当前流程显示意意见量
            if (this.LaiHanLC < eLaiHanLCBH.登记)
            {
                SetEnabled(true);
                this.trYSJ.Visible = true;
            }
            else if (this.LaiHanLC == eLaiHanLCBH.登记)
            {
                SetEnabled(false);
                this.trYSJ.Visible = true;
            }
            else if ((int)this.LaiHanLC >= (int)eLaiHanLCBH.受理)
            {
                SetEnabled(false);
                this.trYSJ.Visible = false;
            }
        }

        /// <summary>
        /// 提交验证
        /// </summary>
        ValidateRS IUCLHOperate.Validate()
        {
            ValidateRS rs = new ValidateRS();
            if ((int)this.LaiHanLC < (int)eLaiHanLCBH.登记)
            {
                //DataRowCollection drc =new YS_SF_ZHICHUXGProcess().SelectYS_SF_ZHICHUXGsDynamic(string.Format("I_JLZT=0 AND I_FEIYONGBH={0} and I_TYPE=320", this.hdCollection.Get("I_FEIYONGBH")), null).Rows;
                //if(drc.Count>0)
                //{
                //    rs.Msg = "托收已退票,不允许登记！";
                //    return rs;
                //}
                //if(this.txtTuoShou.Text=="未到账")
                //{
                //    rs.Msg = "费用未到账,无法退款！";
                //    return rs;
                //}
                if (this.txtTuiKuanJE.Text == "")
                {
                    rs.Msg = "请输入退款金额！";
                    return rs;
                }
                if (!PageTools.ValidateString(this.txtTuiKuanJE.Text, PageTools.eValidateType.正数))
                {
                    rs.Msg = "请输入正确的退款金额！";
                    return rs;
                }
                //if (this.txtTuiKuanJHL.Text.IndexOf(".")>-1)
                //{
                //    rs.Msg = "退款计划量非整数,请输入正确退款金额！";
                //    return rs;
                //}
                if (Convert.ToDecimal(this.txtTuiKuanJE.Text)>Convert.ToDecimal(this.txtYiShouJE.Text))
                {
                    rs.Msg = "退款金额大于已收金额,请检查！";
                    return rs;
                }
                if (this.txtTuiKuanFP.Text == "")
                {
                    rs.Msg = "请选择发票号码！";
                    return rs;
                }
                if (this.txtYinHangZH.Text == "")
                {
                    rs.Msg = "请填写银行账号！";
                    return rs;
                }
                if (this.txtKaiHuYH.Text == "")
                {
                    rs.Msg = "请填写开户银行！";
                    return rs;
                }
                if (this.txtKaiHuHM.Text == "")
                {
                    rs.Msg = "请填写开户户名！";
                    return rs;
                }
            }
            rs.IsSuccess = true;
            return rs;
        }

        /// <summary>
        /// 清空数据
        /// </summary>
        public void ClearInfo()
        {
            this.txtKaiHuHM.Text = "";
            this.txtKaiHuYH.Text = "";
            this.txtKaiZhangRQ.Text = "";
            this.txtTuiKuanFP.Text = "";
            this.txtTuiKuanJE.Text = "";
            this.txtTuiKuanJHL.Text = "";
            this.txtTuoShou.Text = "";
            this.txtYingShouJE.Text = "";
            this.txtYinHangZH.Text = "";
            this.txtYiShouJE.Text = "";
            this.txtYSJ.Text = "";
            this.cmbTuiKuanLX.Text = "";
        }       

        /// <summary>
        /// 根据来函加载信息
        /// </summary>
        /// <param name="laihanxx"></param>
        public void LoadInfo(YS_LH_LAIHANXX laihanxx)
        {
            if (laihanxx.S_YONGHUBH == null)
            {
                return;
            }
            if (laihanxx.ID == 0)
            {
                this.LaiHanLC = eLaiHanLCBH.未登记;
            }
            else {
                this.LaiHanLC = (eLaiHanLCBH)Enum.Parse(typeof(eLaiHanLCBH), laihanxx.I_LIUCHENGBH.ToString());
            }
            //根据当前来函流程显示控件
            SetDisplay();
            int? rsno = -1;
            string rsmsg = "";
            //获取对应年月营业账信息
            DataRowCollection drc = new SpHelper().P_YSLH_TUIKUANXXHQ(laihanxx.S_LAIHANBH, laihanxx.S_YONGHUBH, (int)(laihanxx.I_Y * 100 + laihanxx.I_M), 2, out rsno, out rsmsg).Tables[0].Rows;
            this.txtKaiZhangRQ.Text = Convert.ToDateTime(drc[0]["D_KAIZHANGRQ"]).ToString("yyyy-MM-dd");
            this.txtYingShouJE.Text = drc[0]["N_YINGSHOUJE"].ToString();
            this.txtYiShouJE.Text = drc[0]["N_SHISHOUJE"].ToString();
            this.txtYSJ.Text = drc[0]["N_YSJ"].ToString();
            this.txtKaiHuHM.Text = Convert.ToString(drc[0]["S_KAIHUHM"]);
            this.txtKaiHuYH.Text = Convert.ToString(drc[0]["S_KAIHUYH"]);
            this.txtYinHangZH.Text = Convert.ToString(drc[0]["S_YINHANGZH"]);
            this.txtTuiKuanJHL.Text = Convert.ToString(drc[0]["I_TUIKUANJHL"]);
            this.txtTuiKuanJE.Text = Convert.ToString(drc[0]["N_TUIKUANJE"]);
            this.txtTuiKuanFP.Text = Convert.ToString(drc[0]["S_TUIKUANFPHM"]);
            this.txtTuoShou.Text = Convert.ToString(drc[0]["S_TUOSHOUZT"]);
            if (!Convert.IsDBNull(drc[0]["I_TUIKUANLX"]))
            {
                this.cmbTuiKuanLX.Value = Convert.ToString(drc[0]["I_TUIKUANLX"]);
            }

            this.hdCollection.Set("I_JIAJIABL", drc[0]["I_JIAJIABL"]);
            this.hdCollection.Set("I_FEIYONGBH", drc[0]["I_FEIYONGBH"]);
            ////记录站点,管理块
            //this.ZhanDianBH = laihanxx.S_ZHANDIANBH;
            //this.GuanLiKBH = laihanxx.S_GUANLIKBH;          
        }

        /// <summary>
        /// 返回副表信息
        /// </summary>
        /// <returns></returns>
        public void GreateLHFB(YS_LH_LAIHANXX laihanxx)
        {
            YS_LH_TUIKUAN tz = new YS_LH_TUIKUAN();
            tz.ID = 0;
            tz.I_JLZT = 0;
            tz.S_LAIHANBH = laihanxx.S_LAIHANBH;
            tz.I_SHURUR = LoginInfo.CurrentUser.UserID;
            tz.D_SHURURQ = DateTime.Now;
            tz.I_LIUCHENGBH = laihanxx.I_LIUCHENGBH;
            tz.I_SHENPIJG = 1;
            tz.S_SHENPIYJ = laihanxx.I_LIUCHENGBH==0?"预登记":"登记";
            tz.I_DANGQIAN = 1;
            YS_LH_TUIKUANProcess bllT = new YS_LH_TUIKUANProcess();
            //将当前已存在的记录I_DANGQIAN更新为0
            bllT.UpdateYS_LH_TUIKUANsDynamic("I_DANGQIAN=0", "S_LAIHANBH=" + laihanxx.S_LAIHANBH);
            //插入新的登记信息
            bllT.InsertOrUpdate(tz);
        }

        /// <summary>
        /// 生成退款信息
        /// </summary>
        /// <returns></returns>
        public void CreateShengQingXX(YS_LH_LAIHANXX laihanxx)
        {
            YS_LH_TUIKUANXX obj = new YS_LH_TUIKUANXX();
            obj.D_KAIZHANGRQ = Convert.ToDateTime(this.txtKaiZhangRQ.Text);
            obj.I_TUIKUANJHL = Convert.ToInt32(this.txtTuiKuanJHL.Text);
            obj.I_TUIKUANLX = Convert.ToInt32(this.cmbTuiKuanLX.Value);
            obj.N_SHISHOUJE = Convert.ToDouble(this.txtYiShouJE.Text);
            obj.N_TUIKUANJE = Convert.ToDouble(this.txtTuiKuanJE.Text);
            obj.N_YINGSHOUJE = Convert.ToDouble(this.txtYingShouJE.Text);
            obj.N_YSJ = Convert.ToDouble(this.txtYSJ.Text);
            obj.S_KAIHUHM = this.txtKaiHuHM.Text;
            obj.S_KAIHUYH = this.txtKaiHuYH.Text;
            obj.S_LAIHANBH = laihanxx.S_LAIHANBH;
            obj.S_TUIKUANFPHM = this.txtTuiKuanFP.Text;
            obj.S_TUOSHOUZT = this.txtTuoShou.Text;
            obj.S_YINHANGZH = this.txtYinHangZH.Text;
            obj.I_TUIKUANZT = 0;
            obj.I_FEIYONGBH = Convert.ToInt32(this.hdCollection.Get("I_FEIYONGBH"));
            obj.I_JIAJIABL = Convert.ToInt32(this.hdCollection.Get("I_JIAJIABL"));
            obj.I_DANGQIAN = 1;
            YS_LH_TUIKUANXXProcess bllT = new YS_LH_TUIKUANXXProcess();
            //将当前已存在的记录I_DANGQIAN更新为0
            bllT.UpdateYS_LH_TUIKUANXXesDynamic("I_DANGQIAN=0", "S_LAIHANBH=" + laihanxx.S_LAIHANBH);
            //插入新的登记信息
            bllT.InsertOrUpdate(obj);
        }

        #endregion

    }
}